﻿using System;
using System.Collections.Generic;
using System.Linq;
using Xinkui.WebModel.BasicControl;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.Common;
using Xinkui.WebModel;
using Xinkui.WebModel.Model;
using Xinkui.WebModel.Cache;
using Xinkui.WebModel.Helper;
using Xinkui.WebModel.BasicPage;
using Xinkui.DataAccess.User;

public partial class UserControl_fore_UC_BuyleadsDetail : ForeControl {
    protected int BuyleadsId {
        get {
            return CommonFramework.Utility.WebHelper.GetQueryStringValue<int>("buy", 0);
        }
    }

    public bool IsSellLogined { get; set; }


    public string PostDate { get; set; }
    public string ExpiryDate { get; set; }
    public string PurchaseType { get; set; }

    public string Title { get; set; }
    public string Detail { get; set; }



    protected void Page_Load(object sender, EventArgs e) {

        IsSellLogined = this.Base.LoginUser != null && this.Base.LoginUser.UserID > 0 && this.Base.LoginUser.MemberType == (short)MemberTypeEnum.Seller;



        var buyleadsModel = Singleton<BuyleadInfoDataAccess>.Instance.BuyleadInfo_View(BuyleadsId);
        if (buyleadsModel != null) {

            var rBuyleads = Singleton<BuyleadInfoDataAccess>.Instance.ListRelatedBuylead(6, buyleadsModel.CategoryID);
            if (rBuyleads != null && rBuyleads.Count > 0) {
                this.rptBuyleads.DataSource = rBuyleads;
                this.rptBuyleads.DataBind();
            }
            Title = buyleadsModel.Subject;
            (this.Base as ForePage).Enqueue(NavItem.Create(Title));
            Detail = buyleadsModel.Details;
            PostDate = UIHelper.FormatDate(buyleadsModel.UpdateDate);
            ExpiryDate = UIHelper.FormatDate(buyleadsModel.ValidDate);
            PurchaseType = DictCache.GetName((int)DictTypeEnum.PurchaseType, buyleadsModel.PurchaseType);

            if (IsSellLogined) {
                List<PropertyItem> properies = new List<PropertyItem>();
                var userModel = UserDataAccess.ViewUnionUser(buyleadsModel.BuyleadID);
                if (buyleadsModel.Country > 0) {
                    var c = CountryCache.Get(buyleadsModel.Country);
                    if (c != null) {
                        properies.Add(new PropertyItem {
                            Title = "Country",
                            Value = c.Name

                        });
                    }
                }
                if (userModel != null) {

                    if (!string.IsNullOrEmpty(userModel.CompanyName)) {
                        properies.Add(new PropertyItem {
                            Title = "Company Name",
                            Value = userModel.CompanyName

                        });
                    }
                    if (!string.IsNullOrEmpty(userModel.FirstName) || !string.IsNullOrEmpty(userModel.LastName)) {
                        properies.Add(new PropertyItem {
                            Title = "Contact Person",
                            Value = !string.IsNullOrEmpty(userModel.FirstName) && !string.IsNullOrEmpty(userModel.LastName)
                            ? string.Format("{0} . {1}", userModel.FirstName, userModel.LastName)
                            : (!string.IsNullOrEmpty(userModel.FirstName) ? userModel.FirstName : userModel.LastName)
                        });
                    }

                    if (!string.IsNullOrEmpty(userModel.PhoneNum)) {
                        properies.Add(new PropertyItem {
                            Title = "Telphone Num",
                            Value = userModel.PhoneNum
                        });
                    }
                    if (!string.IsNullOrEmpty(userModel.FaxNum)) {
                        properies.Add(new PropertyItem {
                            Title = "Fax Num",
                            Value = userModel.FaxNum
                        });
                    }

                    this.rptProperties.DataSource = properies;
                    this.rptProperties.DataBind();

                }

            }
            else {
                rptProperties.Visible = false;
            }
        }
    }
}